﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
using Microsoft.Expression.Interactivity.Core;
using Microsoft.Phone.Controls;

namespace Phone7ActionPack.Behaviors
{
    /// <summary>
    /// this behavior is used to use the visual state manager to change state to 
    /// </summary>
    public class OrentationChangedGoToStateBehavior : Behavior<FrameworkElement>
    {
        public string LandscapeStateName { get; set; }
        public string LandscapeLeftStateName { get; set; }
        public string LandscapeRightStateName { get; set; }
        //public string NoneStateName { get; set; }
        public string PortraitStateName { get; set; }
        //public string PortraitDownStateName { get; set; }
        //public string PortraitUpStateName { get; set; }
        public bool UseTransitions { get; set; }
        public bool useExtended;
        public OrentationChangedGoToStateBehavior() {
            
        }

        protected override void OnAttached()
        {
            
            base.OnAttached();
            Action<FrameworkElement> delayedLinkup = a =>
            {
                var el = a.WalkUpTreeTillType<PhoneApplicationPage>();
                if (el != null)
                    el.OrientationChanged += this.OrentationChangedGoToStateBehavior_OrientationChanged;
            };
            try
            {
                AssociatedObject.DelayedCommand(delayedLinkup, 60);
            }
            catch (Exception)
            {
                AssociatedObject.DelayedCommand(delayedLinkup, 60);
            }
        }

        /// <summary>
        /// handles when we detect that the orentation is changed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void OrentationChangedGoToStateBehavior_OrientationChanged(object sender, OrientationChangedEventArgs e)
        {
            string stateName = "";
            switch (e.Orientation)
            {
                //case PageOrientation.Landscape:
                //    stateName = LandscapeStateName;//, UseTransitions);
                //    break;
                case PageOrientation.LandscapeLeft:
                    stateName = this.LandscapeLeftStateName;//, UseTransitions);
                    break;
                case PageOrientation.LandscapeRight:
                    stateName = this.LandscapeRightStateName;//, UseTransitions);
                    break;
                case PageOrientation.None:
                    stateName = this.PortraitStateName;//, UseTransitions);
                    break;
                case PageOrientation.Portrait:
                    stateName = this.PortraitStateName;//, UseTransitions);
                    break;
                case PageOrientation.PortraitDown:
                    stateName = this.PortraitStateName;//, UseTransitions);
                    break;
                case PageOrientation.PortraitUp:
                    stateName = this.PortraitStateName;//, UseTransitions);
                    break;
            }

            if (stateName != null)
                if (useExtended)
                    ExtendedVisualStateManager.GoToElementState(this.AssociatedObject.Parent as FrameworkElement, stateName, UseTransitions);
                else
                    VisualStateManager.GoToState(this.AssociatedObject.WalkUpTreeTillType<Control>(), stateName, UseTransitions);
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            var el = this.AssociatedObject.WalkUpTreeTillType<PhoneApplicationPage>();
            if (el != null)
                el.OrientationChanged += OrentationChangedGoToStateBehavior_OrientationChanged;
        }

        public ICommand MyCommand
        {
            get;
            private set;
        }

        private void MyFunction()
        {
            try
            {
                this.AssociatedObject.WalkUpTreeTillType<PhoneApplicationPage>().OrientationChanged += OrentationChangedGoToStateBehavior_OrientationChanged;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}